<?php
namespace App\Api\Controllers\user;


use App\Api\Controllers\BaseController;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsController extends BaseController
{

    public function goods_list (Request $request)
    {

        try {
            $user = $this->parseToken();
            $user_id = $user->user_id;
            $store_id = $request->get('store_id', '');
            $title = $request->get('title', '');

            $obj = DB::table('store_goods');


            $where = [];
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
            }

            if ($title) {
                $where[] = ['title', 'like', '%' . $title . '%'];

            }

            $code = $request->get('code', '');
            if ($code) {
                $where[] = ['store_goods.code', '=', $code];
            }
            if ($title) {
                $where[] = ['store_goods.title', 'like', '%' . $title . '%'];

            }

            $obj = $obj->where($where)
                ->select('store_goods.*', 'store_goods_cate.title as cate_title')
                ->leftJoin("store_goods_cate","store_goods.store_goods_cate_id","=","store_goods_cate.id")
//                ->orderBy('create_at', 'desc');
                ->orderBy('id', 'desc');

            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function add_goods(Request $request)
    {

        try {
            $user = $this->parseToken();
            $data = $request->except(['token']);
            $check_data = [
                'store_id' => '门店ID',
                'store_goods_cate_id' => '商品分类',
                'title' => '标题',
                'img' => '图片',
                'code' => '编码',
                'price' => '价格',
                'status' => '状态',
                'sort' => '排序',
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            $data['create_at'] = date('Y-m-d H:i:s');
            if (isset($data['id']) && !empty($data['id'])) {
                $id = $data['id'];
                unset($data['id']);

                if (DB::table('store_goods')->whereNotIn('id', [$id])->where(['store_id' => $data['store_id'], 'code' => $data['code']])->first()) {
                    return json_encode([
                        'status' => 2,
                        'message' => $data['code']. '编码已存在'
                    ]);
                }

                DB::table('store_goods')->where(['id' => $id])->update($data);
            } else {

                if (DB::table('store_goods')->where(['store_id' => $data['store_id'], 'code' => $data['code']])->first()) {
                    return json_encode([
                        'status' => 2,
                        'message' => $data['code']. '编码已存在'
                    ]);
                }

                DB::table('store_goods')->insert($data);
            }
            $this->status = 1;
            $this->message = '添加成功';
            return $this->format();

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function del_goods(Request $request)
    {

        try {
            $id = $request->get('id');
            DB::table('store_goods')->where('id', $id)->delete();
            $this->status = 1;
            $this->message = '删除成功';

            return $this->format();

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function goods_info(Request $request)
    {

        try {
            $code = $request->get('code');
            $store_id = $request->get('store_id');
// DB::enableQueryLog();  //开启日志
            $data = DB::table('store_goods')->where(['code' => $code, 'store_id' => $store_id, 'status' => 2])->first();
//             var_dump(DB::getQueryLog());die; //打印日志
//            var_dump($data);
            if ($data) {
                $this->status = 1;
                $this->message = '';
                return $this->format($data);
            } else {
                $this->status = -1;
                $this->message = '暂无数据';
                return $this->format();
            }


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function goods_lists (Request $request)
    {

        try {
            $store_id = $request->get('store_id', '');

            if (empty($store_id)) {
                $this->status = -1;
                $this->message = '缺少必要参数';
                return $this->format();
            }

            $obj = DB::table('store_goods');


            $where = [];
            if ($store_id) {
                $where[] = ['store_id', '=', $store_id];
            }

            $stores['is_goods'] = DB::table('stores')->where('store_id', $store_id)->value('is_goods');
            $stores['store_btn'] = json_decode(json_encode(DB::table('store_goods_btn')->where('store_id', $store_id)->first()), true);


            $obj = $obj->where($where)
                ->select('store_goods.*', 'store_goods_cate.title as cate_title')
                ->leftJoin("store_goods_cate","store_goods.store_goods_cate_id","=","store_goods_cate.id")
                ->orderBy('create_at', 'desc');
            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->order_data = $stores;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function goods_cate ()
    {
        $obj = DB::table('store_goods_cate');
        $obj = $obj->orderBy('create_at', 'desc');
        $this->t = $obj->count();
        $data = $this->page($obj)->get();
        $this->status = 1;
        $this->message = '数据返回成功';
        return $this->format($data);
    }

    public function add_goods_cate(Request $request)
    {
        try {
            $user = $this->parseToken();
            $data = $request->except(['token']);
            $check_data = [
                'title' => '标题'
            ];
            $check = $this->check_required($data, $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }
            $data['create_at'] = date('Y-m-d H:i:s');
            if (isset($data['id']) && !empty($data['id'])) {
                $id = $data['id'];
                unset($data['id']);
                DB::table('store_goods_cate')->where(['id' => $id])->update($data);
            } else {
                DB::table('store_goods_cate')->insert($data);
            }

            $this->status = 1;
            $this->message = '添加成功';
            return $this->format();

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


}
